package org.mte.sak.reflect;


import java.math.BigDecimal;

import org.mte.sak.util.ClassType;
import org.mte.sak.util.Timer;

/**
 * ArgDescriptor: descrive una parametro di un metodo
 *
 * @author Marco Traversari
 * @version $Revision: 1.1 $ $Date: 2011-04-18 08:37:54 $ $Author: marco $
 */
public class ArgDescriptor {
	
    private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

	/**
	 * type
	 */
	private String type = null;

	/**
	 * value
	 */
	private Object value = null;

	/**
	 * ArgDescriptor
	 * @param value
	 */
	public ArgDescriptor(String value) {
		this.type = ClassType.STRING.def;
		this.value = value;
	}

	/**
	 * ArgDescriptor
	 * @param type
	 * @param value
	 */
	public ArgDescriptor(String type, String value) {
		this.type = retrieveTypeDef(type);
		this.value = retrieveValue(value);
	}

	/**
	 * ArgDescriptor
	 * @param type
	 * @param value
	 */
	public ArgDescriptor(String type, Object value) {
		this.type = type;
		this.value = value;
	}

	/**
	 * getType
	 * @return type
	 */
	public String getType() {
		return type;
	}

	/**
	 * setType
	 * @param type
	 */
	public void setType(String type) {
		this.type = retrieveTypeDef(type);
	}

	/**
	 * getValue
	 * @return value
	 */
	public Object getValue() {
		return value;
	}

	/**
	 * setValue
	 * @param value
	 */
	public void setValue(String value) {
		this.value = retrieveValue(value);
	}

	/**
	 * retrieveTypeDef
	 * @param typeName
	 * @return typeDef
	 */
	private String retrieveTypeDef(String typeName) {
		String typeDef = ClassType.STRING.def;
		if (typeName != null) {
			ClassType[] classTypes = ClassType.values();
			int len = classTypes.length;
			boolean isFound = false;
			for (int i = 0; i < len && !isFound; i++) {
				if (typeName.equals(classTypes[i].def)) {
					isFound = true;
					typeDef = classTypes[i].def;
				}
			}
		}
		return typeDef;
	}

	/**
	 * retrieveValue
	 *
	 * <ul>
	 * <li>Se l'oggettto deve essere una data allora il valore
	 * deve essere nel formato di CommonParameter.DATE_DEFAULT_PATTERN</li>
	 * <li>Se l'oggetto deve essere un FileObject allora il valore
	 * deve essere il percorso assoluto.</li>
	 * </ul>
	 *
	 * @param value
	 * @return obj
	 */
	private Object retrieveValue(String value) {
		Object obj = null;
		try {
			if (type.equals(ClassType.STRING.def)) {
				obj = value;
			} else 	if (type.equals(ClassType.INTEGER.def)) {
				obj = new Integer(value);
			} else 	if (type.equals(ClassType.DOUBLE_OBJ.def)) {
				obj = new Double(value);
			} else 	if (type.equals(ClassType.DATE.def)) {
				Timer timer = new Timer();
				timer.setPattern(DEFAULT_DATE_PATTERN);
				obj = timer.getDate(value);
			} else 	if (type.equals(ClassType.LONG_OBJ.def)) {
				obj = new Long(value);
			} else 	if (type.equals(ClassType.BIG_DECIMAL.def)) {
				obj = new BigDecimal(value);
			} else 	if (type.equals(ClassType.BOOLEAN_OBJ.def)) {
				obj = new Boolean(value);
			} else 	if (type.equals(ClassType.FILE_OBJECT.def)) {
				obj = new FileObject(value);
			} else 	if (type.equals(ClassType.BOOLEAN.def)) {
				obj = new Boolean(value);
			} else 	if (type.equals(ClassType.INT.def)) {
				obj = new Integer(value);
			} else 	if (type.equals(ClassType.LONG.def)) {
				obj = new Long(value);
			} else 	if (type.equals(ClassType.DOUBLE.def)) {
				obj = new Double(value);
			} else 	if (type.equals(ClassType.FLOAT.def)) {
				obj = new Float(value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

}
